package schedule;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

public class Schedule {
	String driver ="oracle.jdbc.driver.OracleDriver";
	String url ="jdbc:oracle:thin:@192.168.10.122:1521:xe";
	String db_user = "jobs";
	String db_pwd = "1234";
	
	
	Connection conn;
		
	ScheduleVo vo;
	
	Calendar cal = Calendar.getInstance();
	SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
	SimpleDateFormat sdm = new SimpleDateFormat("MM");
	String sy = sdy.format(new Date());
	String sm = sdm.format(new Date());
	private int year = Integer.parseInt(sy);
	private int month = Integer.parseInt(sm);

	
	public Schedule() {
		try {
			//1) 드라이버 로딩
			Class.forName(driver).newInstance();
			
			//2) Connection 객체생성
			conn = DriverManager.getConnection(url, db_user, db_pwd);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//연도변경 월변경
	public void cngCal(int t) {
		if(t<0){
			if(month==1){
				month=12;
				year--;
			}else{
				month--;
			}
		}else{
			if(month==12){
				month=1;
				year++;
			}
		}
	}
	
	public boolean insert(){
		boolean flag =false;
		String sql = "insert into schedule(serial, subject, mid, content, st_date, ed_date) values (sche_serial.nextval,?,?,?,?)";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, vo.getSubject());
			ps.setString(2, vo.getMid());
			ps.setString(3, vo.getContent());
			ps.setString(4, vo.getMdate());
			
			
			int g = ps.executeUpdate();
			
			if(g>0){
				flag=true;
			}
			
			ps.close();conn.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public Vector<ScheduleVo> list(){
		Vector<ScheduleVo> v = new Vector<>();
		String sql ="select * from schedule where st_date like ? or ed_date like ?";
			try {
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1, vo.getFind());
				ps.setString(2, vo.getFind());
				
				ResultSet rs = ps.executeQuery();
				while(rs.next()){
					ScheduleVo sv = new ScheduleVo();
					
					sv.setSerial(rs.getInt("serial"));
					sv.setSubject(rs.getString("subject"));
					sv.setContent(rs.getString("content"));
					sv.setMdate(rs.getString("mdate"));
					
					v.add(sv);
				}
				
				rs.close();ps.close();conn.close();
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}
	
	
	public ScheduleVo getVo() {
		return vo;
	}

	public void setVo(ScheduleVo vo) {
		this.vo = vo;
	}
	

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

}
